package com.lanju.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.lanju.entity.Money;
import com.lanju.entity.SearchLog;
import com.lanju.entity.SharePrize;

@Repository
public class SharePrizeDao {

	@PersistenceContext(unitName = "financialmarketingUnit")
	private EntityManager entityManager;

	public void insertSharePrize(SharePrize sp) {
		try {
			entityManager.persist(sp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void addMoney(Money m){
		try {
			entityManager.persist(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void mergeMoney(Money m){
		try {
			entityManager.merge(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Money hasMoney(String phone,int type) throws Exception{
		StringBuffer sql = new StringBuffer();
		Money money=null;
		try {
			sql.append(" from Money where phone=?1");
			if(!org.springframework.util.StringUtils.isEmpty(type)){
				sql.append(" and type="+type);
			}
			Query query = entityManager.createQuery(sql.toString());
			query.setParameter(1, phone);
			if(query.getResultList() != null && query.getResultList().size()>0){
				money = (Money) query.getResultList().get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return money;
	}
	public List<SearchLog> hasShareLog(String phone) {
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" from SearchLog where phone=?1");
			Query query = entityManager.createQuery(sql.toString());
			query.setParameter(1, phone);
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<SharePrize> hasSharePrize(String phone) {
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" from SharePrize where phone=?1");
			Query query = entityManager.createQuery(sql.toString());
			query.setParameter(1, phone);
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void inserSearchLog(SearchLog log){
		try {
			entityManager.persist(log);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
